package com.example.util;

import java.util.Arrays;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/14
 */
public class SearchKeywordUtil {
    private String searchKeyword;
    public static String handleSearchKeyword(String searchKeyword){
        //String cleanSearchKeyword = searchKeyword.replaceAll("[^a-zA-Z0-9\\s\\p{IsHan}]+", "").replaceAll("\\s{2,}", " ");

        StringBuilder cleanSearchKeywordBuilder = new StringBuilder();
        for (char c : searchKeyword.toCharArray()) {
            if (Character.isLetterOrDigit(c)) {
                cleanSearchKeywordBuilder.append(c);
            }
        }
        String cleanSearchKeyword = cleanSearchKeywordBuilder.toString();
        return cleanSearchKeyword;
    }

    //计算相似度百分比
    public static double calculatePercentageSimilar(String s1,String s2){
        double maxLength = Math.max(s1.length(),s2.length());
        double mathCount = 0;
        for (int i = 0; i < s1.length(); i++) {
            for (int j = 0; j < s2.length(); j++) {
                if (s1.charAt(i)==s2.charAt(j)){
                    mathCount++;
                }
            }

        }
        System.out.println(mathCount);
        System.out.println(maxLength);
        double percentageSimilar = mathCount/maxLength * 100;
        return percentageSimilar;

    }

    //判读两个字符串是否是另一个字符串的子集
    public static boolean includeSearch(String s1,String s2){
        String maxStr = "";
        String minStr = "";
        if (s1.length()<=s2.length()){
            minStr = s1;
            maxStr = s2;
        }else {
            minStr = s2;
            maxStr = s1;
        }
        boolean isSubset = true;
        for (int i = 0; i < minStr.length(); i++) {
            char c = minStr.charAt(i);
            if (maxStr.indexOf(c) == -1) {
                isSubset = false;
                break;
            }
        }
        return isSubset;
    }

//    public static void main(String[] args) {
//        String s1 = "这是一段话";
//        String s2 = "这话一";
//        System.out.println(includeSearch(s1,s2));
//    }

}
